﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Ccit.OA.MsAddrBook;
using System.Text;
using Ccit.Utils;
namespace Ccit.OA.AddrBook
{
    public partial class addrbook_personal_index : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 ViewState["Query"] = ""; 
            }
            InitPage(); 
        }
        private void InitPage()
        {
            ViewState["Query"] = "";
            if (this.txttName.Text.Trim() != "")
            {
                ViewState["Query"] = txttName.Text.Trim().Replace("'", "''");
            } 
            DataBinde();
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void DataBinde()
        {
            AddrBookBLL Bll = new AddrBookBLL();
            DataSet dSet = Bll.GetAddBookMes(ViewState["Query"].ToString());
            if (dSet.Tables[0].Rows.Count > 0)
            {
                dgMain.DataSource = dSet;
                dgMain.DataBind();
            }
            else
            {
                dgMain.DataSource = null;
                dgMain.DataBind();
            }
        }
        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnbQuery_Click(object sender, EventArgs e)
        {
            DevExpress.Web.Data.WebDataSelection oSelect = dgMain.Selection;
            oSelect.UnselectAll();
            InitPage();
        }
        #region Ajax Methods
        /// <summary>
        /// get budge 
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DoOperate(string strJson)
        { 
            AddrBookBLL Bll = new AddrBookBLL(); 
            string iNum  = Bll.DoOperateBook(strJson);  
            Bll = null; 
            return iNum;
        }
        #endregion

        protected void dgMain_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
            string sRecid = dgMain.GetRowValues(e.VisibleIndex, "PKID").ToString();
            string BZ = dgMain.GetRowValues(e.VisibleIndex, "BZ").ToString();
            string GroupId = dgMain.GetRowValues(e.VisibleIndex, "GroupId").ToString();
            //e.Row.Cells[1].Text = "<img src='" + BZ + "' align='middle' border='0'/>";
            HtmlAnchor lnbEdit = (HtmlAnchor)dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbEdit");
            HtmlImage ImgView = (HtmlImage)dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "ImgView");
            if (ImgView != null)
            {
                ImgView.Src = BZ;
            }
            //string Company = dgMain.GetRowValues(e.VisibleIndex, "Company").ToString();
            //if (Company!="")
            //{
            //    if (Company.Length > 10)
            //    {
            //        e.Row.Cells[3].Text = "<nobr>" + Company.Substring(0, 10) + "...</nobr>";
            //    } 
            //}
            //string email = dgMain.GetRowValues(e.VisibleIndex, "email").ToString();
            //if (email!="")
            //{
            //    if (email.Length > 10)
            //    {
            //        e.Row.Cells[7].Text = "<nobr>" + email.Substring(0, 10) + "...</nobr>";
            //    }
            //} 
            lnbEdit.Attributes["href"] = "javascript:doOperate('" + sRecid + "')";
        }
        /// <summary>
        /// 导出到Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnbExportExcel_Click(object sender, EventArgs e)
        {
            if (_hidTypeID.Value != "")
            {
                Response.ContentType = "application/vnd.ms-excel";
                string ExcelName = "AddBooks(" + Ccit.Utils.DateLib.GetSysCurDateTime(FormatType.date) + ").xls";
                Response.AppendHeader("content-disposition", "attachment;filename=" + ExcelName);
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                StringBuilder sb = new StringBuilder();
                sb.Append("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">\r\n<meta name=ProgId content=Excel.Sheet>\r\n<meta name=Generator content=\"Microsoft Excel 9\">\r\n<link rel=File-List href=\"./Excel_Model.files/filelist.xml\">\r\n<link rel=Edit-Time-Data href=\"./Excel_Model.files/editdata.mso\">\r\n<link rel=OLE-Object-Data href=\"./Excel_Model.files/oledata.mso\">\r\n<!--[if gte mso 9]><xml>\r\n <o:DocumentProperties>\r\n  </o:DocumentProperties>\r\n <o:OfficeDocumentSettings>\r\n  <o:DownloadComponents/>\r\n </o:OfficeDocumentSettings>\r\n</xml><![endif]-->\r\n<style>\r\n<!--table\r\n\t{mso-displayed-decimal-separator:\"\\.\";\r\n\tmso-displayed-thousand-separator:\"\\,\";}\r\n@page\r\n");
                sb.Append("\t{margin:1.0in .75in 1.0in .75in;\r\n\tmso-header-margin:.5in;\r\n\tmso-footer-margin:.5in;}\r\ntr\r\n\t{mso-height-source:auto;\r\n\tmso-ruby-visibility:none;}\r\nxl26\r\n\t{border-top: 0.5pt solid;\r\n\tborder-buttom: 0.5pt solid;\r\n\tborder-left: 0.5pt solid;\r\n\tborder-right: 0.5pt solid;}\r\ncol\r\n\t{mso-width-source:auto;\r\n\tmso-ruby-visibility:none;}\r\nbr\r\n\t{mso-data-placement:same-cell;}\r\n.style0\r\n\t{mso-number-format:General;\r\n\ttext-align:general;\r\n\tvertical-align:bottom;\r\n\twhite-space:nowrap;\r\n\tmso-rotate:0;\r\n\tmso-background-source:auto;\r\n\tmso-pattern:auto;\r\n\tcolor:windowtext;\r\n\tfont-size:12.0pt;\r\n\tfont-weight:400;\r\n\tfont-style:normal;\r\n\ttext-decoration:none;\r\n\tfont-family:KNLe;\r\n\tmso-generic-font-family:auto;\r\n\tmso-font-charset:134;\r\n\tborder:none;\r\n\tmso-protection:locked visible;\r\n\tmso-style-name:3#9f;\r\n\tmso-style-id:0;}\r\ntd\r\n\t{mso-style-parent:style0;\r\n\tpadding-top:1px;\r\n\tpadding-right:1px;\r\n\tpadding-left:1px;\r\n\tmso-ignore:padding;\r\n\tcolor:windowtext;\r\n\tfont-size:12.0pt;\r\n\tfont-weight:400;\r\n\tfont-style:normal;\r\n\ttext-decoration:none");
                sb.Append("\r\n\tfont-family:KNLe;\r\n\tmso-generic-font-family:auto;\r\n\tmso-font-charset:134;\r\n\tmso-number-format:General;\r\n\ttext-align:general;\r\n\tvertical-align:bottom;\r\n\tborder:none;\r\n\tmso-background-source:auto;\r\n\tmso-pattern:auto;\r\n\tmso-protection:locked visible;\r\n\twhite-space:nowrap;\r\n\tmso-rotate:0;}\r\nruby\r\n\t{ruby-align:left;}\r\nrt\r\n\t{color:windowtext;\r\n\tfont-size:9.0pt;\r\n\tfont-weight:400;\r\n\tfont-style:normal;\r\n\ttext-decoration:none;\r\n\tfont-family:KNLe;\r\n\tmso-generic-font-family:auto;\r\n\tmso-font-charset:134;\r\n\tmso-char-type:none;\r\n\tdisplay:none;}\r\n-->\r\n</style>\r\n<!--[if gte mso 9]><xml>\r\n <x:ExcelWorkbook>\r\n  <x:ExcelWorksheets>\r\n   <x:ExcelWorksheet>\r\n    <x:Name>HKJB5508M3<F</x:Name>\r\n    <x:WorksheetOptions>\r\n     <x:DefaultRowHeight>285</x:DefaultRowHeight>\r\n     <x:Selected/>\r\n     <x:TopRowVisible>9</x:TopRowVisible>\r\n     <x:Panes>\r\n      <x:Pane>\r\n       <x:Number>3</x:Number>\r\n       <x:ActiveRow>30</x:ActiveRow>");
                sb.Append("\r\n       <x:ActiveCol>2</x:ActiveCol>\r\n      </x:Pane>\r\n     </x:Panes>\r\n     <x:ProtectContents>False</x:ProtectContents>\r\n     <x:ProtectObjects>False</x:ProtectObjects>\r\n     <x:ProtectScenarios>False</x:ProtectScenarios>\r\n    </x:WorksheetOptions>\r\n   </x:ExcelWorksheet>\r\n  </x:ExcelWorksheets>\r\n  <x:WindowHeight>6525</x:WindowHeight>\r\n  <x:WindowWidth>11700</x:WindowWidth>\r\n  <x:WindowTopX>120</x:WindowTopX>\r\n  <x:WindowTopY>15</x:WindowTopY>\r\n  <x:ProtectStructure>False</x:ProtectStructure>\r\n  <x:ProtectWindows>False</x:ProtectWindows>\r\n </x:ExcelWorkbook>\r\n</xml><![endif]--><!--[if gte mso 9]><xml>\r\n <o:DocumentProperties>  </o:DocumentProperties>\r\n</xml><![endif]-->\r\n</head>\r\n\r\n");
                sb.Append("<body link=blue vlink=purple>");
                sb.Append(" <table x:str border=1 cellpadding=0 cellspacing=0 style='border-collapse:collapse;table-layout:fixed'>\n");
                #region 创建标题
                    sb.Append(" <tr style='height:14.25pt'");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>用户ID</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>名称</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>称谓</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>生日</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>分组</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>单位</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>部门</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>职务</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>手机</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>办公电话</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>家庭电话</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>传真</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>邮寄地址</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>邮编</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>MSN</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>QQ</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>Email</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>标志</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>网址</td>");
                    sb.Append(" <td style='text-align:center;border:0.5pt solid'>备注</td>");
                    sb.Append("</tr>\n");
	            #endregion                
                AddrBookBLL Bll = new AddrBookBLL();
                List<MsAddBooks> lstAddBooks = Bll.GetAddBooksExcelMes(_hidTypeID.Value);
                if(lstAddBooks.Count>0)
                {
                    for (int i = 0; i < lstAddBooks.Count;i++ )
                    {
                        MsAddBooks ms = lstAddBooks[i];
                        sb.Append(" <tr style='height:14.25pt'");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' x:num>" + ms.UserId + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid'>" + ms.Name + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid'>" + ms.Call + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid'>" + ms.Birthday + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' x:num>" + ms.GroupId + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.Company + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.DeptName + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.PostName + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' x:num>" + ms.Telphone + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' x:num>" + ms.Comtel + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' x:num>" + ms.Ftel + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.Fax + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.Maddress + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' x:num>" + ms.Postcode + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.msnname + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' x:num>" + ms.qname + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.email + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.makenum + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.url + "</td>");
                        sb.Append(" <td style='text-align:center;border:0.5pt solid' >" + ms.remark + "</td>");
                        sb.Append("</tr>\n");
                    }
                }
                lstAddBooks = null;
                Bll = null;
                sb.Append("</table>");
                sb.Append("</body>");
                sb.Append("</html>\n"); 
                Response.Write(sb.ToString());
            }
            else
            {
                Response.Write("<script type='text/javascript'>alert('尚未选择您要导出的数据!');</script>");
            }
        }

        //导入Excel
        [global::System.Web.Services.WebMethod]
        public static string SaveImport(string jsonExpr)
        {
            AddrBookBLL bll = new AddrBookBLL();
            List<MsAddBooks> lstAddBook = (List<MsAddBooks>)JavaScriptConvert.DeserializeObject(jsonExpr, typeof(List<MsAddBooks>));
            string  iNum = bll.SaveImportExcelMes(lstAddBook);
            bll = null;
            return iNum;
        }
    }
}
